package com.whc.gulimall.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whc.common.utils.PageUtils;
import com.whc.common.utils.R;
import com.whc.gulimall.member.entity.MemberReceiveAddressEntity;
import com.whc.gulimall.member.service.MemberReceiveAddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 会员收货地址
 *
 * @author wanghaochen
 * @email angaoen@outlook.com
 * @date 2022-03-05 00:23:42
 */
@RestController
@RequestMapping("member/memberreceiveaddress")
public class MemberReceiveAddressController {

    /**
     * 会员收货地址
     */
    @Resource
    private MemberReceiveAddressService memberReceiveAddressService;

    /**
     * 通过memberId获取收货地址列表
     *
     * @param memberId memberId
     * @return 统一返回结果
     */
    @GetMapping("/{memberId}/receiveAddresses")
    public R listReceiveAddressByMemberId(@PathVariable("memberId") Long memberId) {
        List<MemberReceiveAddressEntity> list = memberReceiveAddressService
                .list(new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", memberId));
        return R.ok().setData(list);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:memberreceiveaddress:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberReceiveAddressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 通过attrId获取到收货地址信息
     *
     * @param id 收货地址id
     * @return MemberReceiveAddressEntity
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        return R.ok().setData(memberReceiveAddressService.getById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:memberreceiveaddress:save")
    public R save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.save(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:memberreceiveaddress:update")
    public R update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.updateById(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:memberreceiveaddress:delete")
    public R delete(@RequestBody Long[] ids) {
        memberReceiveAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
